package application.client.jms;

import java.util.Properties;

import javax.naming.*;
import javax.jms.*;

import application.controller.client.GuiController;

public class MyConsumer implements MessageListener {
	private static String IP;
	
	private MessageConsumer _consumer;

	public MyConsumer() throws NamingException, JMSException{
		Properties env = new Properties();
		env.put("org.omg.CORBA.ORBInitialHost", IP); 
		env.put("org.omg.CORBA.ORBInitialPort", "3700"); 
		env.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
		InitialContext ctx = new InitialContext(env);
		QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx
				.lookup("LibManQueueFactory");
		QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
		QueueSession queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
		Queue queue = (Queue) ctx.lookup("LibManQueue");
		queueConnection.start();
		QueueReceiver receiver = queueSession.createReceiver(queue);
		receiver.setMessageListener(this);
		// Close the connection
//		con.close();
//		System.out.println("Connection closed");

	}
	
	public static void setIP(String ip){
		IP = ip;
	}

	@Override
	public void onMessage(Message arg0) {
		// Start the connection, causing message delivery to begin
		// Receive the messages sent to the destination
		TextMessage msg = (TextMessage) arg0;
		try {
			GuiController.getInstance().editMessageTab(msg.getText());
			System.out.println(msg.getText());
		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}